package smlab.log
{
	
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	/**
	 * Class for tracking user behaviour in Google Analytics. Implemented in all Vali games.
	 * 
	 * Once your preloader has finished, call TrackingManager.instance.init("cosl", stage);
	 * Replace cosl with the subdomain-name under which the game will run
	 * 
	 * @author artman
	 */	
	public class TrackingManager {
		/**
		 * The singleton instance of the TrackingManager 
		 */		
		public static var instance:TrackingManager = new TrackingManager();
		private var pingTimer:Timer;
		private var pinged:int = 0;
		private var pingEvery:int = 5 * 60 * 1000;
		private var lastMouseMove:Number = 0;
		private var gameName:String = "";
		private var avatarOnceCreated:Boolean = false;
		private var gameOnceEntered:Boolean = false;
		
		public function TrackingManager() {
		}
		
		/**
		 * Initialized the TrackingManager singleton. 
		 * @param A reference to the stage instance of the running client.
		 */		
		public function init(gameName:String, stage:Stage):void {
			this.gameName = gameName;
			pingTimer = new Timer(pingEvery);
			pingTimer.addEventListener(TimerEvent.TIMER, ping);
			pingTimer.start();
			stage.addEventListener(MouseEvent.MOUSE_MOVE, interactionEvent);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, interactionEvent);
			
			// Set slot 2 custom variable which contains the name of the game beeing played.
			if(ExternalInterface.available) {
				ExternalInterface.call("_gaq.push", ['_setCustomVar', 2, 'Playing game', gameName, 2]);
			}
			sendEvent("Game", "Loading completed");
		}
		
		/**
		 * Call, when a new avatar has been created for the user 
		 */		
		public function avatarCreated():void {
			if(!avatarOnceCreated) {
				avatarOnceCreated = true;
				sendEvent("Game", "Create avatar");
			}
		}
		
		/** 
		 * Call, when the user enters a game space for the first time during a session
		 */		
		public function gameEntered():void {
			if(!gameOnceEntered) {
				gameOnceEntered = true;
				sendEvent("Game", "Enter game");
			}
		}
		
		/**
		 * Track and user event 
		 * @param The category of the event
		 * @param The action of the event
		 * @param The optional label of the event
		 * @param The optional integer value of the event
		 */		
		public function sendEvent(category:String, action:String, label:String = null, value:int = int.MAX_VALUE):void {
			if(ExternalInterface.available) {
				if(label && value != int.MAX_VALUE) {
					var data:Array = [category, action, label, value];
				} else if(label) {
					data = [category, action, label];
				} else {
					data = [category, action];
				}
				var sendEvent:Array = data.concat();
				sendEvent.splice(0,0,"_trackEvent");
				ExternalInterface.call("_gaq.push", sendEvent);
				
				// Send event as pageview
				var url:String = data.join("/").replace(/ /gi, "_");
				var trackPage:Array = ["_trackPageview", "eventTracking/" + url ];
				ExternalInterface.call("_gaq.push", trackPage);
			}
		}
		
		private function interactionEvent(e:Event):void {
			lastMouseMove = getTimer();
		}
		
		private function ping(e:TimerEvent):void {
			if(lastMouseMove + (pingEvery / 1.1) > getTimer()) {
				pinged += pingEvery;
				sendEvent("Game session", "Ping", (pinged / 1000 / 60) + " minutes in game");
			}
		}
	}
}